package ips.statistical_analysis.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import cm.util.hibernate.HibernateSessionFactory;

public class StatisticalAnalysisBarAction {

	private Session session;
    private Transaction trans;
    private String hql;
    private Query query;
    private List l;
    private Session session_1;
    private Transaction trans_1;
    private String hql_1;
    private Query query_1;
    private List l_1;
    private Session session_2;
    private Transaction trans_2;
    private String hql_2;
    private Query query_2;
    private List l_2;
    private Date begindate;
    private Date enddate;
    
    
    public Session getSession_2() {
		return session_2;
	}

	public void setSession_2(Session session_2) {
		this.session_2 = session_2;
	}

	public Transaction getTrans_2() {
		return trans_2;
	}

	public void setTrans_2(Transaction trans_2) {
		this.trans_2 = trans_2;
	}

	public String getHql_2() {
		return hql_2;
	}

	public void setHql_2(String hql_2) {
		this.hql_2 = hql_2;
	}

	public Query getQuery_2() {
		return query_2;
	}

	public void setQuery_2(Query query_2) {
		this.query_2 = query_2;
	}

	public List getL_2() {
		return l_2;
	}

	public void setL_2(List l_2) {
		this.l_2 = l_2;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Transaction getTrans() {
		return trans;
	}

	public void setTrans(Transaction trans) {
		this.trans = trans;
	}

	public String getHql() {
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public Query getQuery() {
		return query;
	}

	public void setQuery(Query query) {
		this.query = query;
	}

	public List getL() {
		return l;
	}

	public void setL(List l) {
		this.l = l;
	}
	

	public Date getBegindate() {
		return begindate;
	}

	public void setBegindate(Date begindate) {
		this.begindate = begindate;
	}

	public Date getEnddate() {
		return enddate;
	}

	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}
	
	public Session getSession_1() {
		return session_1;
	}

	public void setSession_1(Session session_1) {
		this.session_1 = session_1;
	}

	public Transaction getTrans_1() {
		return trans_1;
	}

	public void setTrans_1(Transaction trans_1) {
		this.trans_1 = trans_1;
	}

	public String getHql_1() {
		return hql_1;
	}

	public void setHql_1(String hql_1) {
		this.hql_1 = hql_1;
	}

	public Query getQuery_1() {
		return query_1;
	}

	public void setQuery_1(Query query_1) {
		this.query_1 = query_1;
	}

	public List getL_1() {
		return l_1;
	}

	public void setL_1(List l_1) {
		this.l_1 = l_1;
	}

	public String execute() throws Exception
	{
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
//*******************************包异常检测告警*********************************************/		
		  session=HibernateSessionFactory.getSession();
		  trans=session.beginTransaction();
		  hql="select tm,count(*) from AbnormalDetect as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
		  query=session.createQuery(hql);
		  query.setString("t","abnormal_attack");//设置查询参数
		  l=query.list();
		  for(Iterator sit=l.iterator();sit.hasNext();)//迭代取值
	     {
			Object[] objects=(Object[])sit.next();
			Date date=(Date)objects[0];
			//介于起始时间和终止时间
          if(((begindate.before(date))&&(enddate.after(date)))||(begindate.equals(date))||(enddate.equals(date)))
          {
			SimpleDateFormat format = new SimpleDateFormat("MM月dd日"); 
			String s = format.format(date);
			
			long count=(Long)objects[1];
//			System.out.println("日期"+s);
//		    System.out.println("的病毒数量为"+count);
		    dataset.addValue(count, "数据包异常攻击次数", s);//设置条形图的值
          }
          }
//*******************************流量异常检测告警*********************************************/		
		  session_1=HibernateSessionFactory.getSession();
		  trans_1=session_1.beginTransaction();
		  hql_1="select tm,count(*) from AttackLog as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
		  query_1=session_1.createQuery(hql_1);
		  query_1.setString("t","flow_attack");//设置查询参数
		  l_1=query_1.list();
		  for(Iterator sit_1=l_1.iterator();sit_1.hasNext();)//迭代取值
	     {
			Object[] objects_1=(Object[])sit_1.next();
			Date date_1=(Date)objects_1[0];
			//介于起始时间和终止时间
          if(((begindate.before(date_1))&&(enddate.after(date_1)))||(begindate.equals(date_1))||(enddate.equals(date_1)))
          {
			SimpleDateFormat format_1 = new SimpleDateFormat("MM月dd日"); 
			String s_1 = format_1.format(date_1);
			
			long count_1=(Long)objects_1[1];
		    dataset.addValue(count_1, "流量异常攻击次数", s_1);//设置条形图的值
          }
          }
//*******************************滥用检测告警*********************************************/		
		  session_2=HibernateSessionFactory.getSession();
		  trans_2=session_2.beginTransaction();
		  hql_2="select tm,count(*) from AttackLog as al where al.type=:t group by year(al.tm),month(al.tm),day(al.tm)";
		  query_2=session_2.createQuery(hql_2);
		  query_2.setString("t","abuse_attack");//设置查询参数
		  l_2=query_2.list();
	//	  DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		  for(Iterator sit_2=l_2.iterator();sit_2.hasNext();)//迭代取值
	     {
			Object[] objects_2=(Object[])sit_2.next();
			Date date_2=(Date)objects_2[0];
			//介于起始时间和终止时间
          if(((begindate.before(date_2))&&(enddate.after(date_2)))||(begindate.equals(date_2))||(enddate.equals(date_2)))
          {
			SimpleDateFormat format_2 = new SimpleDateFormat("MM月dd日"); 
			String s_2 = format_2.format(date_2);
			
			long count_2=(Long)objects_2[1];
//			System.out.println("日期"+s);
//		    System.out.println("的病毒数量为"+count);
		    dataset.addValue(count_2, "滥用攻击次数", s_2);//设置条形图的值
          }
          }
////////////////////////////		  
		  SimpleDateFormat yue = new SimpleDateFormat( 
		  "yyyy年MM月dd日"); 
		  String y1 = yue.format(begindate); 
		  String y2 = yue.format(enddate);
		  JFreeChart chart = ChartFactory.createBarChart3D(y1+"至"+y2+"攻击告警柱形图分析", // chart title
				    "日期", // domain axis label
				    "攻击次数", // range axis label
				    dataset, // data
				    PlotOrientation.VERTICAL, // orientation
				    true, // include legend
				    true, // tooltips
				    false // urls
				    );
		  	chart.getTitle().setFont(new Font("宋体",Font.BOLD,15));
			CategoryPlot plot = chart.getCategoryPlot(); 
			NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
			vn.setAutoRangeIncludesZero(true); 

			CategoryAxis domainAxis = plot.getDomainAxis(); 

			domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
			// Lable 
			Font labelFont = new Font("宋体", Font.BOLD, 12); 

			domainAxis.setLabelFont(labelFont);
			domainAxis.setTickLabelFont(labelFont);

			domainAxis.setMaximumCategoryLabelWidthRatio(1f);
			// domainAxis.setVerticalCategoryLabels(false); 
			plot.setDomainAxis(domainAxis); 

			ValueAxis rangeAxis = plot.getRangeAxis(); 
			rangeAxis.setLabelFont(labelFont);
			rangeAxis.setTickLabelFont(labelFont);
			rangeAxis.setUpperMargin(0.15); 
			rangeAxis.setLowerMargin(0.15); 
			plot.setRangeAxis(rangeAxis); 
			BarRenderer renderer = new BarRenderer(); 
			renderer.setMaximumBarWidth(0.7); 
			renderer.setMinimumBarLength(10); 

			renderer.setBaseOutlinePaint(Color.BLACK); 

			renderer.setSeriesPaint(0, Color.GREEN); 
			renderer.setSeriesPaint(1, new Color(0, 0, 255)); 
			renderer.setItemMargin(0); 
			renderer 
			.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
			renderer.setBaseItemLabelsVisible(true); 

			plot.setRenderer(renderer); 
			plot.setForegroundAlpha(0.6f); 
			plot.setBackgroundAlpha(0.7f);
			
			chart.getLegend().setItemFont(labelFont);
	        
			HttpServletResponse response = ServletActionContext.getResponse();
			try {
				ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart,
						700, 300);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		  
		  session.close();
		  System.out.println("成功");
				return null;
		 } 
}
